package demo;

import java.net.InetAddress;
import java.net.URI;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Hashtable;

import org.apache.log4j.Logger;

public class ServerImpl implements ServerInterface {
	private static final Logger log = Logger.getLogger(ServerImpl.class
			.getName());
//	private HashMap<String, ClientInterface> clients;
	private ServerInterface otherServer;
	private String name;
	private URI uri;
	private Hashtable<Integer, ClientInterface> clients;

	

	public ServerImpl(String name, URI uri) {
		super();
		this.name = name;
		this.uri = uri;
	//	clients = new HashMap<String, ClientInterface>();
		clients = new Hashtable<Integer, ClientInterface>();
	}

	@Override
	public boolean login(Integer number, ClientInterface client)
			throws RemoteException {
		clients.put(number, client);
		System.out.println("user " + name + " login....");
		return true;
	}

	@Override
	public void synchronize(HashMap<String, ClientInterface> clients)
			throws RemoteException {

	}

	@Override
	public void synchronize(Integer nubmer, ClientInterface client)
			throws RemoteException {
		System.out.println("synchronize ....");
		clients.put(nubmer, client);
		System.out.println("call client call back");
		try {
			client.connectWithMe(this.name, uri);
			System.out.println("call client,connecting with me");
		} catch (RemoteException e) {
			e.printStackTrace();
		}

	}

	private void init() {

	}

	public Hashtable<Integer, ClientInterface> getClients() throws RemoteException{
		return clients;
	}

	public void setClients(Hashtable<Integer, ClientInterface> clients) {
		this.clients = clients;
	}

	public ServerInterface getOtherServer() {
		return otherServer;
	}

	public void setOtherServer(ServerInterface otherServer) {
		this.otherServer = otherServer;
	}


	

}
